package org.example;

import java.util.ArrayDeque;
import java.util.Deque;

//leetcode 用队列实现栈 https://leetcode.cn/problems/implement-stack-using-queues/description/
class MyStack {
    Deque<Integer> queue1;
    Deque<Integer> queue2;
    public MyStack() {
        queue1 = new ArrayDeque<>();
        queue2 = new ArrayDeque<>();
    }

    public void push(int x) {
        queue1.add(x);
    }

    public int pop() {
        int ret = -1;
        if (!queue1.isEmpty()) {
            while (queue1.size() > 1) {
                queue2.add(queue1.poll());
            }
            ret = queue1.poll();
            while (!queue2.isEmpty()) {
                queue1.add(queue2.poll());
            }
        }
        return ret;
    }

    public int top() {
        int ret = -1;
        if (!queue1.isEmpty()) {
            while (queue1.size() > 0) {
                ret = queue1.poll();
                queue2.add(ret);
            }
            while (!queue2.isEmpty()) {
                queue1.add(queue2.poll());
            }
        }
        return ret;
    }

    public boolean empty() {
        return queue1.isEmpty();
    }
}

public class Test2 {

}
